package com.shoping.common;

import com.shoping.common.health.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Slf4j
@Configuration
@EnableScheduling
@ConditionalOnWebApplication
@EnableConfigurationProperties({ContextProperties.class})
public class ShoppingAutoConfiguration {

    private final ContextProperties contextProperties;


    public ShoppingAutoConfiguration(ContextProperties contextProperties) {
        this.contextProperties = contextProperties;
    }

    @Bean
    public DiagnosticHealthStatusChangeListener diagnosticHealthStatusChangeListener() {
        return new DiagnosticHealthStatusChangeListener();
    }

    @Bean
    public WebApplicationListener meteorWebApplicationListener() {
        return new WebApplicationListener();
    }

    @Bean
    public HealthStatusChecker healthStatusChecker() {
        return new HealthStatusChecker();
    }

    @Bean
    @ConditionalOnMissingBean
    public SpringHealthIndicator meteorHealthIndicator() {
        log.info("context = {}", contextProperties);
        return new SpringHealthIndicator(contextProperties.getInitStatus());
    }
}
